package com.mytool.util;

import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 代码解析工具类
 */
@Component
public class CodeParser {

    /**
     * 解析生成的代码内容，提取文件路径和内容
     *
     * @param generatedContent AI生成的代码内容
     * @return 文件路径和内容的映射，key为文件路径，value为文件内容
     */
    public Map<String, String> parseGeneratedCode(String generatedContent) {
        Map<String, String> filesMap = new HashMap<>();
        
        // 匹配 ```java 和 ``` 之间的代码块
        Pattern codeBlockPattern = Pattern.compile("```java\\s*\\n// (.+?)\\s*\\n([\\s\\S]*?)```", Pattern.DOTALL);
        Matcher matcher = codeBlockPattern.matcher(generatedContent);
        
        while (matcher.find()) {
            String filePath = matcher.group(1).trim();
            String fileContent = matcher.group(2);
            
            // 将文件路径和内容添加到映射中
            filesMap.put(filePath, fileContent);
        }
        
        return filesMap;
    }
    
    /**
     * 解析DDL，提取表信息和字段信息
     *
     * @param ddlSql DDL SQL语句
     * @return 表信息的映射
     */
    public Map<String, Object> parseDDLInfo(String ddlSql) {
        Map<String, Object> tableInfo = new HashMap<>();
        
        // 提取表名和表注释
        Pattern tablePattern = Pattern.compile("CREATE\\s+TABLE\\s+[`'\"]?(\\w+)[`'\"]?\\s*\\(([\\s\\S]*?)\\)\\s*(?:ENGINE=[\\w\\d]+)?(?:\\s*DEFAULT\\s*CHARSET=[\\w\\d]+)?(?:\\s*COMMENT\\s*=\\s*['\"]([^'\"]*)['\"])?", Pattern.CASE_INSENSITIVE);
        Matcher tableMatcher = tablePattern.matcher(ddlSql);
        
        if (tableMatcher.find()) {
            String tableName = tableMatcher.group(1);
            String columnsDefinition = tableMatcher.group(2);
            String tableComment = tableMatcher.group(3);
            
            tableInfo.put("tableName", tableName);
            tableInfo.put("tableComment", tableComment != null ? tableComment : "");
            
            // 解析列定义
            Map<String, Map<String, String>> columns = parseColumns(columnsDefinition);
            tableInfo.put("columns", columns);
        }
        
        return tableInfo;
    }
    
    /**
     * 解析列定义
     */
    private Map<String, Map<String, String>> parseColumns(String columnsDefinition) {
        Map<String, Map<String, String>> columns = new HashMap<>();
        
        // 匹配列定义
        Pattern columnPattern = Pattern.compile("`(\\w+)`\\s+(\\w+(?:\\(\\d+(?:,\\d+)?\\))?)\\s*(NOT\\s+NULL)?\\s*(DEFAULT\\s+[^,]+)?\\s*(?:COMMENT\\s+['\"]([^'\"]*)['\"])?[,]?", Pattern.CASE_INSENSITIVE);
        Matcher columnMatcher = columnPattern.matcher(columnsDefinition);
        
        while (columnMatcher.find()) {
            String columnName = columnMatcher.group(1);
            String columnType = columnMatcher.group(2);
            String notNull = columnMatcher.group(3);
            String defaultValue = columnMatcher.group(4);
            String comment = columnMatcher.group(5);
            
            Map<String, String> columnInfo = new HashMap<>();
            columnInfo.put("name", columnName);
            columnInfo.put("type", columnType);
            columnInfo.put("required", notNull != null ? "true" : "false");
            columnInfo.put("defaultValue", defaultValue != null ? defaultValue.replace("DEFAULT ", "") : "");
            columnInfo.put("comment", comment != null ? comment : "");
            
            columns.put(columnName, columnInfo);
        }
        
        return columns;
    }
} 